package net.supcmdr.dnd5e;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Toolkit;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;

import net.supcmdr.dnd5e.ui.AdventureNotesPanel;
import net.supcmdr.dnd5e.ui.BackgroundPanel;
import net.supcmdr.dnd5e.ui.CharacterPanel;
import net.supcmdr.dnd5e.ui.EquipmentPanel;
import net.supcmdr.dnd5e.ui.SpellsPanel;
import net.supcmdr.dnd5e.ui.menu.MenuBar;
import net.supcmdr.dnd5e.layoutmanagers.WrapLayout;

public class AppMain {


	//-------------
	// VARIABLES
	//-------------

	JFrame mainFrame;
	MenuBar menuBar;

	int appWidth = 860;
	int appHeight = 725;

	String characterName = new String("Nikxi");



	//--------
	// MAIN
	//--------

	public static void main(String[] args) {

		System.out.println("Launching app...");

		EventQueue.invokeLater(new Runnable() {
			public void run(){
				try{

					UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");

					UIManager.getLookAndFeelDefaults().put("defaultFont", new Font("Tahoma", Font.PLAIN, 12));
					UIManager.getLookAndFeelDefaults().put("TextArea.contentMargins", new Insets(2,4,0,0));
					UIManager.getLookAndFeelDefaults().put("TextField.contentMargins", new Insets(1,3,0,0));

					System.out.println("Loading...");
					AppMain app = new AppMain();
					app.mainFrame.setVisible(true);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		});
	}



	//--------------
	// CONSTRUCTOR
	//--------------

	private AppMain(){
		createDatabase();
		loadData();
		initialize();
	}



	//----------
	// METHODS
	//----------

	private void createDatabase(){
		/*
		 *   IF (Database !exists)
		 *   	create
		 *   Else
		 *      continue
		 * 
		 */
	}

	private void loadData(){
		System.out.println("Loading Character Data");
	}

	private void initialize(){

		// Get screen resolution
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		double width = screenSize.getWidth();
		double height = screenSize.getHeight();


		// Create Frame and set size parameters
		mainFrame = new JFrame("D&D 5E Character Manager           Character: "+ characterName);
		mainFrame.setBounds(((int)width-appWidth)/4, ((int)height-appHeight)/3, appWidth, appHeight);
		mainFrame.setMinimumSize(new Dimension(500,400));

		// Set Default Close Operation
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Create and add Menu Bar
		menuBar = new MenuBar(this, mainFrame);

		// Create the JTabbedPane
		JTabbedPane tabbedPane = new JTabbedPane();
		tabbedPane.setBorder(null);
		tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		tabbedPane.setTabPlacement(JTabbedPane.LEFT);


		// Create the Character panel
		CharacterPanel characterPanel = new CharacterPanel();
		characterPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

		// Create and Apply the WrapLayout to the Character panel
		WrapLayout wrapLayoutCP = new WrapLayout();
		wrapLayoutCP.setAlignment(FlowLayout.CENTER);
		characterPanel.setLayout(wrapLayoutCP);

		// Create the Scroll Pane for the Character panel
		JScrollPane scrollPaneCP = new JScrollPane(characterPanel);
		scrollPaneCP.setAlignmentY(Component.TOP_ALIGNMENT);
		scrollPaneCP.setAlignmentX(Component.LEFT_ALIGNMENT);
		scrollPaneCP.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		scrollPaneCP.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
		scrollPaneCP.getVerticalScrollBar().setUnitIncrement(16);
		scrollPaneCP.getHorizontalScrollBar().setUnitIncrement(16);
		scrollPaneCP.setPreferredSize(new Dimension(600,655));
		tabbedPane.addTab("Character", scrollPaneCP);

		// Create the Equipment panel
		EquipmentPanel equipmentPanel = new EquipmentPanel();
		equipmentPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

		// Create and Apply the WrapLayout to the Equipment panel
		WrapLayout wrapLayoutEP = new WrapLayout();
		wrapLayoutEP.setAlignment(FlowLayout.CENTER);
		equipmentPanel.setLayout(wrapLayoutEP);

		// Create Scroll Pane for the Equipment panel
		JScrollPane scrollPaneEP = new JScrollPane(equipmentPanel);
		scrollPaneEP.setAlignmentY(Component.TOP_ALIGNMENT);
		scrollPaneEP.setAlignmentX(Component.LEFT_ALIGNMENT);
		scrollPaneEP.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		scrollPaneEP.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
		scrollPaneEP.getVerticalScrollBar().setUnitIncrement(16);
		scrollPaneEP.getHorizontalScrollBar().setUnitIncrement(16);
		scrollPaneEP.setPreferredSize(new Dimension(600,655));
		tabbedPane.addTab("Equipment", scrollPaneEP);

		// Create the Spells panel
		JComponent spellsPanel = new SpellsPanel();

		// Apply the WrapLayout to the Spells panel
		WrapLayout wrapLayoutSP = new WrapLayout();
		wrapLayoutSP.setAlignment(FlowLayout.CENTER);
		spellsPanel.setLayout(wrapLayoutSP);

		// Create Scroll Pane for the Spells panel
		JScrollPane scrollPaneSP = new JScrollPane(spellsPanel);
		scrollPaneSP.setAlignmentY(Component.TOP_ALIGNMENT);
		scrollPaneSP.setAlignmentX(Component.LEFT_ALIGNMENT);
		scrollPaneSP.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		scrollPaneSP.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
		scrollPaneSP.getVerticalScrollBar().setUnitIncrement(16);
		scrollPaneSP.getHorizontalScrollBar().setUnitIncrement(16);
		scrollPaneSP.setPreferredSize(new Dimension(600,655));
		tabbedPane.addTab("Spells", scrollPaneSP);

		// Create the Background panel
		JComponent backgroundPanel = new BackgroundPanel();

		// Apply the WrapLayout to the Background panel
		WrapLayout wrapLayoutBP = new WrapLayout();
		wrapLayoutBP.setAlignment(FlowLayout.CENTER);
		backgroundPanel.setLayout(wrapLayoutBP);

		// Create Scroll Pane for the Background panel
		JScrollPane scrollPaneBP = new JScrollPane(backgroundPanel);
		scrollPaneBP.setAlignmentY(Component.TOP_ALIGNMENT);
		scrollPaneBP.setAlignmentX(Component.LEFT_ALIGNMENT);
		scrollPaneBP.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		scrollPaneBP.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
		scrollPaneBP.getVerticalScrollBar().setUnitIncrement(16);
		scrollPaneBP.getHorizontalScrollBar().setUnitIncrement(16);
		scrollPaneBP.setPreferredSize(new Dimension(600,655));
		tabbedPane.addTab("Background", scrollPaneBP);

		// Create the Adventure Notes panel
		JComponent adventureNotesPanel = new AdventureNotesPanel();

		// Apply the WrapLayout to the Adventure Notes panel
		WrapLayout wrapLayoutANP = new WrapLayout();
		wrapLayoutANP.setAlignment(FlowLayout.CENTER);
		adventureNotesPanel.setLayout(wrapLayoutANP);

		// Create Scroll Pane for the Adventure Notes panel
		JScrollPane scrollPaneANP = new JScrollPane(adventureNotesPanel);
		scrollPaneANP.setAlignmentY(Component.TOP_ALIGNMENT);
		scrollPaneANP.setAlignmentX(Component.LEFT_ALIGNMENT);
		scrollPaneANP.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		scrollPaneANP.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
		scrollPaneANP.getVerticalScrollBar().setUnitIncrement(16);
		scrollPaneANP.getHorizontalScrollBar().setUnitIncrement(16);
		scrollPaneANP.setPreferredSize(new Dimension(600,655));
		tabbedPane.addTab("Adventure Notes", scrollPaneANP);

		// Add the Tabbed Pane to the frame
		mainFrame.getContentPane().add(tabbedPane, BorderLayout.CENTER);

	}

	public JFrame getMainFrame(){
		return mainFrame;
	}
}
